package logger

import "fmt"

var log Logger

type Logger interface {
	Debug(format string, v ...interface{})
	Warn(format string, v ...interface{})
	Error(format string, v ...interface{})
}

type Brush func(string) string

func NewBrush(color string) Brush {
	pre := "\033["
	reset := "\033[0m"
	return func(text string) string {
		return pre + color + "m" + text + reset
	}
}

var colors = []Brush{
	NewBrush("1;34"), // Debug    blue
	NewBrush("1;33"), // Warn     yellow
	NewBrush("1;31"), // Error    red
}

type printLog struct {
}

func (p *printLog) Debug(format string, v ...interface{}) {
	fmt.Println("Debug " + colors[0](fmt.Sprintf(format, v...)))
}

func (p *printLog) Warn(format string, v ...interface{}) {
	fmt.Println("Warn " + colors[1](fmt.Sprintf(format, v...)))
}

func (p *printLog) Error(format string, v ...interface{}) {
	fmt.Println("Error " + colors[2](fmt.Sprintf(format, v...)))
}

type nilLog struct {
}

func (n *nilLog) Debug(format string, v ...interface{}) {
}

func (n *nilLog) Warn(format string, v ...interface{}) {
}

func (n *nilLog) Error(format string, v ...interface{}) {
}

func SetLog(l Logger) {
	log = l
}

func SetPrintLog() {
	log = &printLog{}
}

func init() {
	log = &nilLog{}
}

func Debug(format string, v ...interface{}) {
	log.Debug(format, v...)
}

func Warn(format string, v ...interface{}) {
	log.Warn(format, v...)
}

func Error(format string, v ...interface{}) {
	log.Error(format, v...)
}
